adc208c2ae9c0f6c99e0f28c32a668ae0db72441
[platform/kernel/linux-rpi.git] / arch / arm64 / mm / mmap.c
1 /*
2  * Based on arch/arm/mm/mmap.c
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/elf.h>
20 #include <linux/fs.h>
21 #include <linux/memblock.h>
22 #include <linux/mm.h>
23 #include <linux/mman.h>
24 #include <linux/export.h>
25 #include <linux/shm.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sched/mm.h>
28 #include <linux/io.h>
29 #include <linux/personality.h>
30 #include <linux/random.h>
31
32 #include <asm/cputype.h>
33
34 /*
35  * Leave enough space between the mmap area and the stack to honour ulimit in
36  * the face of randomisation.
37  */
38 #define MIN_GAP (SZ_128M + ((STACK_RND_MASK << PAGE_SHIFT) + 1))
39 #define MAX_GAP (STACK_TOP/6*5)
40
41 static int mmap_is_legacy(void)
42 {
43         if (current->personality & ADDR_COMPAT_LAYOUT)
44                 return 1;
45
46         if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
47                 return 1;
48
49         return sysctl_legacy_va_layout;
50 }
51
52 unsigned long arch_mmap_rnd(void)
53 {
54         unsigned long rnd;
55
56 #ifdef CONFIG_COMPAT
57         if (test_thread_flag(TIF_32BIT))
58                 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
59         else
60 #endif
61                 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
62         return rnd << PAGE_SHIFT;
63 }
64
65 static unsigned long mmap_base(unsigned long rnd)
66 {
67         unsigned long gap = rlimit(RLIMIT_STACK);
68
69         if (gap < MIN_GAP)
70                 gap = MIN_GAP;
71         else if (gap > MAX_GAP)
72                 gap = MAX_GAP;
73
74         return PAGE_ALIGN(STACK_TOP - gap - rnd);
75 }
76
77 /*
78  * This function, called very early during the creation of a new process VM
79  * image, sets up which VM layout function to use:
80  */
81 void arch_pick_mmap_layout(struct mm_struct *mm)
82 {
83         unsigned long random_factor = 0UL;
84
85         if (current->flags & PF_RANDOMIZE)
86                 random_factor = arch_mmap_rnd();
87
88         /*
89          * Fall back to the standard layout if the personality bit is set, or
90          * if the expected stack growth is unlimited:
91          */
92         if (mmap_is_legacy()) {
93                 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
94                 mm->get_unmapped_area = arch_get_unmapped_area;
95         } else {
96                 mm->mmap_base = mmap_base(random_factor);
97                 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
98         }
99 }
100
101 /*
102  * You really shouldn't be using read() or write() on /dev/mem.  This might go
103  * away in the future.
104  */
105 int valid_phys_addr_range(phys_addr_t addr, size_t size)
106 {
107         /*
108          * Check whether addr is covered by a memory region without the
109          * MEMBLOCK_NOMAP attribute, and whether that region covers the
110          * entire range. In theory, this could lead to false negatives
111          * if the range is covered by distinct but adjacent memory regions
112          * that only differ in other attributes. However, few of such
113          * attributes have been defined, and it is debatable whether it
114          * follows that /dev/mem read() calls should be able traverse
115          * such boundaries.
116          */
117         return memblock_is_region_memory(addr, size) &&
118                memblock_is_map_memory(addr);
119 }
120
121 /*
122  * Do not allow /dev/mem mappings beyond the supported physical range.
123  */
124 int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
125 {
126         return !(((pfn << PAGE_SHIFT) + size) & ~PHYS_MASK);
127 }
128
129 #ifdef CONFIG_STRICT_DEVMEM
130
131 #include <linux/ioport.h>
132
133 /*
134  * devmem_is_allowed() checks to see if /dev/mem access to a certain address
135  * is valid. The argument is a physical page number.  We mimic x86 here by
136  * disallowing access to system RAM as well as device-exclusive MMIO regions.
137  * This effectively disable read()/write() on /dev/mem.
138  */
139 int devmem_is_allowed(unsigned long pfn)
140 {
141         if (iomem_is_exclusive(pfn << PAGE_SHIFT))
142                 return 0;
143         if (!page_is_ram(pfn))
144                 return 1;
145         return 0;
146 }
147
148 #endif