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
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mem_map.h"
#include "logging.h"
#include "mman.h"
#include <zircon/process.h>
#include <zircon/syscalls.h>
namespace art {
static zx_handle_t fuchsia_lowmem_vmar = ZX_HANDLE_INVALID;
static zx_vaddr_t fuchsia_lowmem_base = 0;
static size_t fuchsia_lowmem_size = 0;
static const char map_name[] = "mmap-android";
static constexpr uintptr_t FUCHSIA_LOWER_MEM_START = 0x80000000;
static constexpr uintptr_t FUCHSIA_LOWER_MEM_SIZE = 0x60000000;
void MemMap::TargetMMapInit() {
if (fuchsia_lowmem_vmar != ZX_HANDLE_INVALID) {
return;
}
zx_info_vmar_t vmarinfo;
CHECK_EQ(zx_object_get_info(zx_vmar_root_self(),
ZX_INFO_VMAR,
&vmarinfo,
sizeof(vmarinfo),
nullptr,
nullptr), ZX_OK) << "could not find info from root vmar";
uintptr_t lower_mem_start = FUCHSIA_LOWER_MEM_START - vmarinfo.base;
fuchsia_lowmem_size = FUCHSIA_LOWER_MEM_SIZE;
uint32_t allocflags = ZX_VM_FLAG_CAN_MAP_READ |
ZX_VM_FLAG_CAN_MAP_WRITE |
ZX_VM_FLAG_CAN_MAP_EXECUTE |
ZX_VM_FLAG_SPECIFIC;
CHECK_EQ(zx_vmar_allocate(zx_vmar_root_self(),
lower_mem_start,
fuchsia_lowmem_size,
allocflags,
&fuchsia_lowmem_vmar,
&fuchsia_lowmem_base), ZX_OK) << "could not allocate lowmem vmar";
}
void* MemMap::TargetMMap(void* start, size_t len, int prot, int flags, int fd, off_t fd_off) {
zx_status_t status;
uintptr_t mem = 0;
bool mmap_lower = (flags & MAP_32BIT) != 0;
// for file-based mapping use system library
if ((flags & MAP_ANONYMOUS) == 0) {
if (start != nullptr) {
flags |= MAP_FIXED;
}
CHECK(!mmap_lower) << "cannot map files into low memory for Fuchsia";
return mmap(start, len, prot, flags, fd, fd_off);
}
uint32_t vmarflags = 0;
if ((prot & PROT_READ) != 0) {
vmarflags |= ZX_VM_FLAG_PERM_READ;
}
if ((prot & PROT_WRITE) != 0) {
vmarflags |= ZX_VM_FLAG_PERM_WRITE;
}
if ((prot & PROT_EXEC) != 0) {
vmarflags |= ZX_VM_FLAG_PERM_EXECUTE;
}
if (len == 0) {
errno = EINVAL;
return MAP_FAILED;
}
zx_info_vmar_t vmarinfo;
size_t vmaroffset = 0;
if (start != nullptr) {
vmarflags |= ZX_VM_FLAG_SPECIFIC;
status = zx_object_get_info((mmap_lower ? fuchsia_lowmem_vmar : zx_vmar_root_self()),
ZX_INFO_VMAR,
&vmarinfo,
sizeof(vmarinfo),
nullptr,
nullptr);
if (status < 0 || reinterpret_cast<uintptr_t>(start) < vmarinfo.base) {
errno = EINVAL;
return MAP_FAILED;
}
vmaroffset = reinterpret_cast<uintptr_t>(start) - vmarinfo.base;
}
zx_handle_t vmo;
if (zx_vmo_create(len, 0, &vmo) < 0) {
errno = ENOMEM;
return MAP_FAILED;
}
zx_vmo_get_size(vmo, &len);
zx_object_set_property(vmo, ZX_PROP_NAME, map_name, strlen(map_name));
if (mmap_lower) {
status = zx_vmar_map(fuchsia_lowmem_vmar, vmaroffset, vmo, fd_off, len, vmarflags, &mem);
} else {
status = zx_vmar_map(zx_vmar_root_self(), vmaroffset, vmo, fd_off, len, vmarflags, &mem);
}
zx_handle_close(vmo);
if (status != ZX_OK) {
return MAP_FAILED;
}
return reinterpret_cast<void *>(mem);
}
int MemMap::TargetMUnmap(void* start, size_t len) {
uintptr_t addr = reinterpret_cast<uintptr_t>(start);
zx_handle_t alloc_vmar = zx_vmar_root_self();
if (addr >= fuchsia_lowmem_base && addr < fuchsia_lowmem_base + fuchsia_lowmem_size) {
alloc_vmar = fuchsia_lowmem_vmar;
}
zx_status_t status = zx_vmar_unmap(alloc_vmar, addr, len);
if (status < 0) {
errno = EINVAL;
return -1;
}
return 0;
}
} // namespace art
|