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 USE_FIXED_ADDR_MEM_CHUNK
51 buf = (unsigned char *)mmap((void *)StartAddr, size,
52 PROT_READ | PROT_WRITE,
53 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
54 -1, 0);
55 #else
56 buf = (unsigned char *)mmap(0, size,
57 PROT_READ | PROT_WRITE,
58 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
59 -1, 0);
60 #endif
61
62 if (buf == MAP_FAILED) {
63 return false;
64 }
65
66 #if USE_FIXED_ADDR_MEM_CHUNK
67 StartAddr += (size + 4095) / 4096 * 4096;
68 #endif
69
70 buf_size = size;
71 return true;
72 }
73
print() const74 void MemChunk::print() const {
75 if (buf != MAP_FAILED) {
76 dump_hex(buf, buf_size, 0, buf_size);
77 }
78 }
79
protect(int prot)80 bool MemChunk::protect(int prot) {
81 if (buf_size > 0) {
82 int ret = mprotect((void *)buf, buf_size, prot);
83 if (ret == -1) {
84 llvm::errs() << "Error: Can't mprotect.\n";
85 return false;
86 }
87
88 if (prot & PROT_EXEC) {
89 FLUSH_CPU_CACHE(buf, buf + buf_size);
90 }
91 }
92
93 return true;
94 }
95