blob: 5d6c1028dc434134e7d723bba520c574d9b4bcf8 [file] [log] [blame]
Stephen Hinesb53c8a52013-04-05 22:17:36 -07001/*
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
Raphael Moll1033b592013-07-23 19:03:29 -070024#ifndef USE_MINGW /* TODO create a proper HAVE_MMAN_H */
Stephen Hinesb53c8a52013-04-05 22:17:36 -070025#include <sys/mman.h>
Raphael Moll1033b592013-07-23 19:03:29 -070026#else
27#include "mmanWindows.h"
28#endif
Stephen Hinesb53c8a52013-04-05 22:17:36 -070029
30#include <stdlib.h>
31
32#ifndef MAP_32BIT
33#define MAP_32BIT 0
34// Note: If the <sys/mman.h> does not come with MAP_32BIT, then we
35// define it as zero, so that it won't manipulate the flags.
36#endif
37
38//#define USE_FIXED_ADDR_MEM_CHUNK 1
39
40#if USE_FIXED_ADDR_MEM_CHUNK
41static uintptr_t StartAddr = 0x7e000000UL;
42#endif
43
Stephen Hinesa221f562013-07-10 17:59:47 -070044AllocFunc MemChunk::VendorAlloc = NULL;
45FreeFunc MemChunk::VendorFree = NULL;
46
47MemChunk::MemChunk() : buf(NULL), buf_size(0), bVendorBuf(true) {
Stephen Hinesb53c8a52013-04-05 22:17:36 -070048}
49
50MemChunk::~MemChunk() {
Stephen Hinesa221f562013-07-10 17:59:47 -070051 if (!invalidBuf() && bVendorBuf && VendorFree) {
52 (*VendorFree)(buf);
53 return;
54 }
55 if (!invalidBuf()) {
Stephen Hinesb53c8a52013-04-05 22:17:36 -070056 munmap(buf, buf_size);
57 }
58}
59
Stephen Hinesa221f562013-07-10 17:59:47 -070060bool MemChunk::invalidBuf() const {
61 return (buf == 0 || buf == (unsigned char *)MAP_FAILED);
62}
63
Stephen Hinesb53c8a52013-04-05 22:17:36 -070064bool MemChunk::allocate(size_t size) {
65 if (size == 0) {
66 return true;
67 }
Stephen Hinesa221f562013-07-10 17:59:47 -070068 if (VendorAlloc) {
69 buf = (unsigned char*)(*VendorAlloc)(size, 0);
70 }
71 if (invalidBuf()) {
72 bVendorBuf = false;
Stephen Hinesb53c8a52013-04-05 22:17:36 -070073#if USE_FIXED_ADDR_MEM_CHUNK
Stephen Hinesa221f562013-07-10 17:59:47 -070074 buf = (unsigned char *)mmap((void *)StartAddr, size,
75 PROT_READ | PROT_WRITE,
76 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
77 -1, 0);
Stephen Hinesb53c8a52013-04-05 22:17:36 -070078#else
Stephen Hinesa221f562013-07-10 17:59:47 -070079 buf = (unsigned char *)mmap(0, size,
80 PROT_READ | PROT_WRITE,
81 MAP_PRIVATE | MAP_ANON | MAP_32BIT,
82 -1, 0);
Stephen Hinesb53c8a52013-04-05 22:17:36 -070083#endif
Stephen Hinesa221f562013-07-10 17:59:47 -070084 }
Stephen Hinesb53c8a52013-04-05 22:17:36 -070085
Stephen Hinesa221f562013-07-10 17:59:47 -070086 if (invalidBuf()) {
Stephen Hinesb53c8a52013-04-05 22:17:36 -070087 return false;
88 }
89
90#if USE_FIXED_ADDR_MEM_CHUNK
91 StartAddr += (size + 4095) / 4096 * 4096;
92#endif
93
94 buf_size = size;
95 return true;
96}
97
98void MemChunk::print() const {
Stephen Hinesa221f562013-07-10 17:59:47 -070099 if (!invalidBuf()) {
Stephen Hinesb53c8a52013-04-05 22:17:36 -0700100 dump_hex(buf, buf_size, 0, buf_size);
101 }
102}
103
104bool MemChunk::protect(int prot) {
105 if (buf_size > 0) {
106 int ret = mprotect((void *)buf, buf_size, prot);
107 if (ret == -1) {
108 llvm::errs() << "Error: Can't mprotect.\n";
109 return false;
110 }
111
112 if (prot & PROT_EXEC) {
113 FLUSH_CPU_CACHE(buf, buf + buf_size);
114 }
115 }
116
117 return true;
118}