blob: da82c25301e7774bcbe3058129b574c7496efba8 [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>
Ingo Molnar01042602017-02-08 18:51:31 +010013#include <linux/sched/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/fs.h>
16#include <linux/elf-fdpic.h>
David Howells5616df22007-05-08 20:27:02 -070017#include <asm/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*****************************************************************************/
20/*
21 * lay out the userspace VM according to our grand design
22 */
23#ifdef CONFIG_MMU
24void elf_fdpic_arch_lay_out_mm(struct elf_fdpic_params *exec_params,
25 struct elf_fdpic_params *interp_params,
26 unsigned long *start_stack,
27 unsigned long *start_brk)
28{
29 *start_stack = 0x02200000UL;
30
31 /* if the only executable is a shared object, assume that it is an interpreter rather than
32 * a true executable, and map it such that "ld.so --list" comes out right
33 */
34 if (!(interp_params->flags & ELF_FDPIC_FLAG_PRESENT) &&
35 exec_params->hdr.e_type != ET_EXEC
36 ) {
37 exec_params->load_addr = PAGE_SIZE;
38
39 *start_brk = 0x80000000UL;
40 }
41 else {
42 exec_params->load_addr = 0x02200000UL;
43
44 if ((exec_params->flags & ELF_FDPIC_FLAG_ARRANGEMENT) ==
45 ELF_FDPIC_FLAG_INDEPENDENT
46 ) {
47 exec_params->flags &= ~ELF_FDPIC_FLAG_ARRANGEMENT;
48 exec_params->flags |= ELF_FDPIC_FLAG_CONSTDISP;
49 }
50 }
51
52} /* end elf_fdpic_arch_lay_out_mm() */
53#endif
54
55/*****************************************************************************/
56/*
57 * place non-fixed mmaps firstly in the bottom part of memory, working up, and then in the top part
58 * of memory, working down
59 */
60unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len,
61 unsigned long pgoff, unsigned long flags)
62{
63 struct vm_area_struct *vma;
Michel Lespinassee759a792013-02-27 17:02:51 -080064 struct vm_unmapped_area_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (len > TASK_SIZE)
67 return -ENOMEM;
68
Benjamin Herrenschmidt2fd3beb2007-05-06 14:50:07 -070069 /* handle MAP_FIXED */
70 if (flags & MAP_FIXED)
71 return addr;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /* only honour a hint if we're not going to clobber something doing so */
74 if (addr) {
75 addr = PAGE_ALIGN(addr);
76 vma = find_vma(current->mm, addr);
77 if (TASK_SIZE - len >= addr &&
78 (!vma || addr + len <= vma->vm_start))
79 goto success;
80 }
81
82 /* search between the bottom of user VM and the stack grow area */
Michel Lespinassee759a792013-02-27 17:02:51 -080083 info.flags = 0;
84 info.length = len;
85 info.low_limit = PAGE_SIZE;
86 info.high_limit = (current->mm->start_stack - 0x00200000);
87 info.align_mask = 0;
88 info.align_offset = 0;
89 addr = vm_unmapped_area(&info);
90 if (!(addr & ~PAGE_MASK))
91 goto success;
92 VM_BUG_ON(addr != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* search from just above the WorkRAM area to the top of memory */
Michel Lespinassee759a792013-02-27 17:02:51 -080095 info.low_limit = PAGE_ALIGN(0x80000000);
96 info.high_limit = TASK_SIZE;
97 addr = vm_unmapped_area(&info);
98 if (!(addr & ~PAGE_MASK))
99 goto success;
100 VM_BUG_ON(addr != -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#if 0
103 printk("[area] l=%lx (ENOMEM) f='%s'\n",
Josef Sipek1f70cec2006-12-08 02:37:01 -0800104 len, filp ? filp->f_path.dentry->d_name.name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106 return -ENOMEM;
107
108 success:
109#if 0
110 printk("[area] l=%lx ad=%lx f='%s'\n",
Josef Sipek1f70cec2006-12-08 02:37:01 -0800111 len, addr, filp ? filp->f_path.dentry->d_name.name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113 return addr;
114} /* end arch_get_unmapped_area() */