1 /*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "MemChunk.h"
18
19 #include "utils/flush_cpu_cache.h"
20 #include "utils/helper.h"
21
22 #include <llvm/Support/raw_ostream.h>
23
24 #include <sys/mman.h>
25
26 #include <stdlib.h>
27
28 #ifndef MAP_32BIT
29 #define MAP_32BIT 0
30 // Note: If the <sys/mman.h> does not come with MAP_32BIT, then we
31 // define it as zero, so that it won't manipulate the flags.
32 #endif
33
34 //#define USE_FIXED_ADDR_MEM_CHUNK 1
35
36 #if USE_FIXED_ADDR_MEM_CHUNK
37 static uintptr_t StartAddr = 0x7e000000UL;
38 #endif
39
MemChunk()40 MemChunk::MemChunk() : buf((unsigned char *)MAP_FAILED), buf_size(0) {
41 }
42
~MemChunk()43 MemChunk::~MemChunk() {
44 if (buf != MAP_FAILED) {
45 munmap(buf, buf_size);
46 }
47 }
48
allocate(size_t size)49 bool MemChunk::allocate(size_t size) {
50 if (size == 0) {
51 return true;
52 }
53 #if USE_FIXED_ADDR_MEM_CHUNK
54 buf = (unsigned char *)mmap((void *)StartAddr, size,
55 PROT_READ | PROT_WRITE,
56 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
57 -1, 0);
58 #else
59 buf = (unsigned char *)mmap(0, size,
60 PROT_READ | PROT_WRITE,
61 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
62 -1, 0);
63 #endif
64
65 if (buf == MAP_FAILED) {
66 return false;
67 }
68
69 #if USE_FIXED_ADDR_MEM_CHUNK
70 StartAddr += (size + 4095) / 4096 * 4096;
71 #endif
72
73 buf_size = size;
74 return true;
75 }
76
print() const77 void MemChunk::print() const {
78 if (buf != MAP_FAILED) {
79 dump_hex(buf, buf_size, 0, buf_size);
80 }
81 }
82
protect(int prot)83 bool MemChunk::protect(int prot) {
84 if (buf_size > 0) {
85 int ret = mprotect((void *)buf, buf_size, prot);
86 if (ret == -1) {
87 llvm::errs() << "Error: Can't mprotect.\n";
88 return false;
89 }
90
91 if (prot & PROT_EXEC) {
92 FLUSH_CPU_CACHE(buf, buf + buf_size);
93 }
94 }
95
96 return true;
97 }
98