1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/mm/mmap.c
5 * Copyright (C) 2012 ARM Ltd.
10 #include <linux/memblock.h>
12 #include <linux/mman.h>
13 #include <linux/export.h>
14 #include <linux/shm.h>
15 #include <linux/sched/signal.h>
16 #include <linux/sched/mm.h>
18 #include <linux/personality.h>
19 #include <linux/random.h>
21 #include <asm/cputype.h>
24 * Leave enough space between the mmap area and the stack to honour ulimit in
25 * the face of randomisation.
27 #define MIN_GAP (SZ_128M)
28 #define MAX_GAP (STACK_TOP/6*5)
30 static int mmap_is_legacy(struct rlimit
*rlim_stack
)
32 if (current
->personality
& ADDR_COMPAT_LAYOUT
)
35 if (rlim_stack
->rlim_cur
== RLIM_INFINITY
)
38 return sysctl_legacy_va_layout
;
41 unsigned long arch_mmap_rnd(void)
46 if (test_thread_flag(TIF_32BIT
))
47 rnd
= get_random_long() & ((1UL << mmap_rnd_compat_bits
) - 1);
50 rnd
= get_random_long() & ((1UL << mmap_rnd_bits
) - 1);
51 return rnd
<< PAGE_SHIFT
;
54 static unsigned long mmap_base(unsigned long rnd
, struct rlimit
*rlim_stack
)
56 unsigned long gap
= rlim_stack
->rlim_cur
;
57 unsigned long pad
= (STACK_RND_MASK
<< PAGE_SHIFT
) + stack_guard_gap
;
59 /* Values close to RLIM_INFINITY can overflow. */
65 else if (gap
> MAX_GAP
)
68 return PAGE_ALIGN(STACK_TOP
- gap
- rnd
);
72 * This function, called very early during the creation of a new process VM
73 * image, sets up which VM layout function to use:
75 void arch_pick_mmap_layout(struct mm_struct
*mm
, struct rlimit
*rlim_stack
)
77 unsigned long random_factor
= 0UL;
79 if (current
->flags
& PF_RANDOMIZE
)
80 random_factor
= arch_mmap_rnd();
83 * Fall back to the standard layout if the personality bit is set, or
84 * if the expected stack growth is unlimited:
86 if (mmap_is_legacy(rlim_stack
)) {
87 mm
->mmap_base
= TASK_UNMAPPED_BASE
+ random_factor
;
88 mm
->get_unmapped_area
= arch_get_unmapped_area
;
90 mm
->mmap_base
= mmap_base(random_factor
, rlim_stack
);
91 mm
->get_unmapped_area
= arch_get_unmapped_area_topdown
;
96 * You really shouldn't be using read() or write() on /dev/mem. This might go
99 int valid_phys_addr_range(phys_addr_t addr
, size_t size
)
102 * Check whether addr is covered by a memory region without the
103 * MEMBLOCK_NOMAP attribute, and whether that region covers the
104 * entire range. In theory, this could lead to false negatives
105 * if the range is covered by distinct but adjacent memory regions
106 * that only differ in other attributes. However, few of such
107 * attributes have been defined, and it is debatable whether it
108 * follows that /dev/mem read() calls should be able traverse
111 return memblock_is_region_memory(addr
, size
) &&
112 memblock_is_map_memory(addr
);
116 * Do not allow /dev/mem mappings beyond the supported physical range.
118 int valid_mmap_phys_addr_range(unsigned long pfn
, size_t size
)
120 return !(((pfn
<< PAGE_SHIFT
) + size
) & ~PHYS_MASK
);
123 #ifdef CONFIG_STRICT_DEVMEM
125 #include <linux/ioport.h>
128 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
129 * is valid. The argument is a physical page number. We mimic x86 here by
130 * disallowing access to system RAM as well as device-exclusive MMIO regions.
131 * This effectively disable read()/write() on /dev/mem.
133 int devmem_is_allowed(unsigned long pfn
)
135 if (iomem_is_exclusive(pfn
<< PAGE_SHIFT
))
137 if (!page_is_ram(pfn
))