1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/* vmix.c: VIRTUAL MEMORY MAPPING (POSIX)
*
* $Id$
* Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license.
*
* .purpose: This is the implementation of the virtual memory mapping
* interface (vm.h) for Unix-like operating systems. It was created
* by copying vmfr.c (the FreeBSD) implementation (as that seemed to
* use the most standards conforming interfaces). vmfr.c was itself
* copied from vli.c (Linux) which was itself copied from vmo1.c (OSF/1
* / DIGITAL UNIX / Tru64).
*
* .deployed: Currently used on Darwin (macOS) and FreeBSD.
*
* .design: <design/vm>. .design.mmap: mmap(2) is used to
* reserve address space by creating a mapping with page access none.
* mmap(2) is used to map pages onto store by creating a copy-on-write
* (MAP_PRIVATE) mapping with the flag MAP_ANON.
*
* .non-standard: Note that the MAP_ANON flag is non-standard; it is
* available on Darwin and FreeBSD. .non-standard.linux: Linux seems
* to use MAP_ANONYMOUS instead. Some Linux systems make MAP_ANON
* available and deprecate it. .non-standard.sesame: On Linux getting
* a definition of MAP_ANON requires a _BSD_SOURCE to be defined prior
* to <sys/mman.h>; see config.h.
*
* .assume.not-last: The implementation of VMInit assumes that
* mmap() will not choose a region which contains the last page
* in the address space, so that the limit of the mapped area
* is representable.
*
* .assume.mmap.err: ENOMEM is the only error we really expect to
* get from mmap. The others are either caused by invalid params
* or features we don't use. See mmap(2) for details.
*
* .remap: Possibly this should use mremap to reduce the number of
* distinct mappings. According to our current testing, it doesn't
* seem to be a problem.
*/
#include "mpm.h"
#if !defined(MPS_OS_FR) && !defined(MPS_OS_LI) && !defined(MPS_OS_XC)
#error "vmix.c is specific to MPS_OS_FR, MPS_OS_LI or MPS_OS_XC"
#endif
#include "vm.h"
#include <errno.h> /* errno */
#include <signal.h> /* sig_atomic_t */
#include <sys/mman.h> /* see .feature.li in config.h */
#include <sys/types.h> /* mmap, munmap */
#include <unistd.h> /* sysconf, _SC_PAGESIZE */
SRCID(vmix, "$Id$");
/* PageSize -- return operating system page size */
Size PageSize(void)
{
long pageSize;
/* Find out the operating system page size
(see design.mps.vm.impl.ix.page.size) */
pageSize = sysconf(_SC_PAGESIZE);
/* Check the page size will fit in a Size. */
AVER((unsigned long)pageSize <= (unsigned long)(Size)-1);
return (Size)pageSize;
}
Res VMParamFromArgs(void *params, size_t paramSize, ArgList args)
{
AVER(params != NULL);
AVERT(ArgList, args);
UNUSED(paramSize);
return ResOK;
}
/* VMInit -- reserve some virtual address space, and create a VM structure */
Res VMInit(VM vm, Size size, Size grainSize, void *params)
{
Size pageSize, reserved;
void *vbase;
AVER(vm != NULL);
AVERT(ArenaGrainSize, grainSize);
AVER(size > 0);
AVER(params != NULL);
pageSize = PageSize();
/* Grains must consist of whole pages. */
AVER(grainSize % pageSize == 0);
/* Check that the rounded-up sizes will fit in a Size. */
size = SizeRoundUp(size, grainSize);
if (size < grainSize || size > (Size)(size_t)-1)
return ResRESOURCE;
reserved = size + grainSize - pageSize;
if (reserved < grainSize || reserved > (Size)(size_t)-1)
return ResRESOURCE;
/* See .assume.not-last. */
vbase = mmap(0, reserved,
PROT_NONE, MAP_ANON | MAP_PRIVATE,
-1, 0);
/* On Darwin the MAP_FAILED return value is not documented, but does
* work. MAP_FAILED _is_ documented by POSIX.
*/
if (vbase == MAP_FAILED) {
int e = errno;
AVER(e == ENOMEM); /* .assume.mmap.err */
return ResRESOURCE;
}
vm->pageSize = pageSize;
vm->block = vbase;
vm->base = AddrAlignUp(vbase, grainSize);
vm->limit = AddrAdd(vm->base, size);
AVER(vm->base < vm->limit); /* .assume.not-last */
AVER(vm->limit <= AddrAdd((Addr)vm->block, reserved));
vm->reserved = reserved;
vm->mapped = 0;
vm->sig = VMSig;
AVERT(VM, vm);
EVENT3(VMInit, vm, VMBase(vm), VMLimit(vm));
return ResOK;
}
/* VMFinish -- release all address space and finish VM structure */
void VMFinish(VM vm)
{
int r;
AVERT(VM, vm);
/* Descriptor must not be stored inside its own VM at this point. */
AVER(PointerAdd(vm, sizeof *vm) <= vm->block
|| PointerAdd(vm->block, VMReserved(vm)) <= (Pointer)vm);
/* All address space must have been unmapped. */
AVER(VMMapped(vm) == (Size)0);
EVENT1(VMFinish, vm);
vm->sig = SigInvalid;
r = munmap(vm->block, vm->reserved);
AVER(r == 0);
}
/* Value to use for protection of newly allocated pages.
* We use a global variable and not a constant so that we can clear
* the executable flag from future requests if Apple Hardened Runtime
* is detected. See <design/vm#impl.xc.prot.exec> for details. */
static sig_atomic_t vm_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
/* VMMap -- map the given range of memory */
Res VMMap(VM vm, Addr base, Addr limit)
{
Size size;
void *result;
AVERT(VM, vm);
AVER(sizeof(void *) == sizeof(Addr));
AVER(base < limit);
AVER(base >= VMBase(vm));
AVER(limit <= VMLimit(vm));
AVER(AddrIsAligned(base, vm->pageSize));
AVER(AddrIsAligned(limit, vm->pageSize));
size = AddrOffset(base, limit);
result = mmap((void *)base, (size_t)size, (int)vm_prot,
MAP_ANON | MAP_PRIVATE | MAP_FIXED,
-1, 0);
if (MAYBE_HARDENED_RUNTIME && result == MAP_FAILED && errno == EACCES
&& (vm_prot & PROT_WRITE) && (vm_prot & PROT_EXEC))
{
/* Apple Hardened Runtime is enabled, so that we cannot have
* memory that is simultaneously writable and executable. Handle
* this by dropping the executable part of the request. See
* <design/vm#impl.xc.prot.exec> for details. */
vm_prot = PROT_READ | PROT_WRITE;
result = mmap((void *)base, (size_t)size, vm_prot,
MAP_ANON | MAP_PRIVATE | MAP_FIXED,
-1, 0);
}
if (result == MAP_FAILED) {
AVER(errno == ENOMEM); /* .assume.mmap.err */
return ResMEMORY;
}
vm->mapped += size;
AVER(VMMapped(vm) <= VMReserved(vm));
EVENT3(VMMap, vm, base, limit);
return ResOK;
}
/* VMUnmap -- unmap the given range of memory */
void VMUnmap(VM vm, Addr base, Addr limit)
{
Size size;
void *addr;
AVERT(VM, vm);
AVER(base < limit);
AVER(base >= VMBase(vm));
AVER(limit <= VMLimit(vm));
AVER(AddrIsAligned(base, vm->pageSize));
AVER(AddrIsAligned(limit, vm->pageSize));
size = AddrOffset(base, limit);
AVER(size <= VMMapped(vm));
/* see <design/vmo1#.fun.unmap.offset> */
addr = mmap((void *)base, (size_t)size,
PROT_NONE, MAP_ANON | MAP_PRIVATE | MAP_FIXED,
-1, 0);
AVER(addr == (void *)base);
vm->mapped -= size;
EVENT3(VMUnmap, vm, base, limit);
}
/* C. COPYRIGHT AND LICENSE
*
* Copyright (C) 2001-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
|