blob: 836f14707a627f156343154f359ac1f7758e9fd4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* elf-fdpic.c: ELF FDPIC memory layout management
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/sched.h>
13#include <linux/mm.h>
14#include <linux/fs.h>
15#include <linux/elf-fdpic.h>
David Howells5616df22007-05-08 20:27:02 -070016#include <asm/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/*****************************************************************************/
19/*
20 * lay out the userspace VM according to our grand design
21 */
22#ifdef CONFIG_MMU
23void elf_fdpic_arch_lay_out_mm(struct elf_fdpic_params *exec_params,
24 struct elf_fdpic_params *interp_params,
25 unsigned long *start_stack,
26 unsigned long *start_brk)
27{
28 *start_stack = 0x02200000UL;
29
30 /* if the only executable is a shared object, assume that it is an interpreter rather than
31 * a true executable, and map it such that "ld.so --list" comes out right
32 */
33 if (!(interp_params->flags & ELF_FDPIC_FLAG_PRESENT) &&
34 exec_params->hdr.e_type != ET_EXEC
35 ) {
36 exec_params->load_addr = PAGE_SIZE;
37
38 *start_brk = 0x80000000UL;
39 }
40 else {
41 exec_params->load_addr = 0x02200000UL;
42
43 if ((exec_params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
44 ELF_FDPIC_FLAG_INDEPENDENT
45 ) {
46 exec_params->flags &= ~ELF_FDPIC_FLAG_ARRANGEMENT;
47 exec_params->flags |= ELF_FDPIC_FLAG_CONSTDISP;
48 }
49 }
50
51} /* end elf_fdpic_arch_lay_out_mm() */
52#endif
53
54/*****************************************************************************/
55/*
56 * place non-fixed mmaps firstly in the bottom part of memory, working up, and then in the top part
57 * of memory, working down
58 */
59unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len,
60 unsigned long pgoff, unsigned long flags)
61{
62 struct vm_area_struct *vma;
Michel Lespinassee759a792013-02-27 17:02:51 -080063 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (len > TASK_SIZE)
66 return -ENOMEM;
67
Benjamin Herrenschmidt2fd3beb2007-05-06 14:50:07 -070068 /* handle MAP_FIXED */
69 if (flags & MAP_FIXED)
70 return addr;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* only honour a hint if we're not going to clobber something doing so */
73 if (addr) {
74 addr = PAGE_ALIGN(addr);
75 vma = find_vma(current->mm, addr);
76 if (TASK_SIZE - len >= addr &&
77 (!vma || addr + len <= vma->vm_start))
78 goto success;
79 }
80
81 /* search between the bottom of user VM and the stack grow area */
Michel Lespinassee759a792013-02-27 17:02:51 -080082 info.flags = 0;
83 info.length = len;
84 info.low_limit = PAGE_SIZE;
85 info.high_limit = (current->mm->start_stack - 0x00200000);
86 info.align_mask = 0;
87 info.align_offset = 0;
88 addr = vm_unmapped_area(&info);
89 if (!(addr & ~PAGE_MASK))
90 goto success;
91 VM_BUG_ON(addr != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* search from just above the WorkRAM area to the top of memory */
Michel Lespinassee759a792013-02-27 17:02:51 -080094 info.low_limit = PAGE_ALIGN(0x80000000);
95 info.high_limit = TASK_SIZE;
96 addr = vm_unmapped_area(&info);
97 if (!(addr & ~PAGE_MASK))
98 goto success;
99 VM_BUG_ON(addr != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#if 0
102 printk("[area] l=%lx (ENOMEM) f='%s'\n",
Josef Sipek1f70cec2006-12-08 02:37:01 -0800103 len, filp ? filp->f_path.dentry->d_name.name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105 return -ENOMEM;
106
107 success:
108#if 0
109 printk("[area] l=%lx ad=%lx f='%s'\n",
Josef Sipek1f70cec2006-12-08 02:37:01 -0800110 len, addr, filp ? filp->f_path.dentry->d_name.name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
112 return addr;
113} /* end arch_get_unmapped_area() */