blob: c7c3d6d5bd17919f57a52418321573d574acfcda [file] [log] [blame]
Nicolas Capensc07dc4b2018-08-06 14:20:45 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Memory.hpp"
16
17#include "Types.hpp"
18#include "Debug.hpp"
19
20#if defined(_WIN32)
21 #ifndef WIN32_LEAN_AND_MEAN
22 #define WIN32_LEAN_AND_MEAN
23 #endif
24 #include <windows.h>
25 #include <intrin.h>
26#else
27 #include <errno.h>
28 #include <sys/mman.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31#endif
32
33#include <memory.h>
34
35#undef allocate
36#undef deallocate
37
38#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined (_M_X64)) && !defined(__x86__)
39#define __x86__
40#endif
41
42namespace rr
43{
44namespace
45{
46struct Allocation
47{
48// size_t bytes;
49 unsigned char *block;
50};
51
52void *allocateRaw(size_t bytes, size_t alignment)
53{
54 ASSERT((alignment & (alignment - 1)) == 0); // Power of 2 alignment.
55
56 #if defined(LINUX_ENABLE_NAMED_MMAP)
57 void *allocation;
58 int result = posix_memalign(&allocation, alignment, bytes);
59 if(result != 0)
60 {
61 errno = result;
62 allocation = nullptr;
63 }
64 return allocation;
65 #else
66 unsigned char *block = new unsigned char[bytes + sizeof(Allocation) + alignment];
67 unsigned char *aligned = nullptr;
68
69 if(block)
70 {
71 aligned = (unsigned char*)((uintptr_t)(block + sizeof(Allocation) + alignment - 1) & -(intptr_t)alignment);
72 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
73
74 // allocation->bytes = bytes;
75 allocation->block = block;
76 }
77
78 return aligned;
79 #endif
80}
81
82#if defined(LINUX_ENABLE_NAMED_MMAP)
83// Create a file descriptor for anonymous memory with the given
84// name. Returns -1 on failure.
85// TODO: remove once libc wrapper exists.
86int memfd_create(const char* name, unsigned int flags)
87{
88 #if __aarch64__
89 #define __NR_memfd_create 279
90 #elif __arm__
91 #define __NR_memfd_create 279
92 #elif __powerpc64__
93 #define __NR_memfd_create 360
94 #elif __i386__
95 #define __NR_memfd_create 356
96 #elif __x86_64__
97 #define __NR_memfd_create 319
98 #endif /* __NR_memfd_create__ */
99 #ifdef __NR_memfd_create
100 // In the event of no system call this returns -1 with errno set
101 // as ENOSYS.
102 return syscall(__NR_memfd_create, name, flags);
103 #else
104 return -1;
105 #endif
106}
107
108// Returns a file descriptor for use with an anonymous mmap, if
109// memfd_create fails, -1 is returned. Note, the mappings should be
110// MAP_PRIVATE so that underlying pages aren't shared.
111int anonymousFd()
112{
113 static int fd = memfd_create("SwiftShader JIT", 0);
114 return fd;
115}
116
117// Ensure there is enough space in the "anonymous" fd for length.
118void ensureAnonFileSize(int anonFd, size_t length)
119{
120 static size_t fileSize = 0;
121 if(length > fileSize)
122 {
123 ftruncate(anonFd, length);
124 fileSize = length;
125 }
126}
127#endif // defined(LINUX_ENABLE_NAMED_MMAP)
128
129} // anonymous namespace
130
131size_t memoryPageSize()
132{
133 static int pageSize = 0;
134
135 if(pageSize == 0)
136 {
137 #if defined(_WIN32)
138 SYSTEM_INFO systemInfo;
139 GetSystemInfo(&systemInfo);
140 pageSize = systemInfo.dwPageSize;
141 #else
142 pageSize = sysconf(_SC_PAGESIZE);
143 #endif
144 }
145
146 return pageSize;
147}
148
149void *allocate(size_t bytes, size_t alignment)
150{
151 void *memory = allocateRaw(bytes, alignment);
152
153 if(memory)
154 {
155 memset(memory, 0, bytes);
156 }
157
158 return memory;
159}
160
161void deallocate(void *memory)
162{
163 #if defined(LINUX_ENABLE_NAMED_MMAP)
164 free(memory);
165 #else
166 if(memory)
167 {
168 unsigned char *aligned = (unsigned char*)memory;
169 Allocation *allocation = (Allocation*)(aligned - sizeof(Allocation));
170
171 delete[] allocation->block;
172 }
173 #endif
174}
175
176void *allocateExecutable(size_t bytes)
177{
178 size_t pageSize = memoryPageSize();
179 size_t length = (bytes + pageSize - 1) & ~(pageSize - 1);
180 void *mapping;
181
182 #if defined(LINUX_ENABLE_NAMED_MMAP)
183 // Try to name the memory region for the executable code,
184 // to aid profilers.
185 int anonFd = anonymousFd();
186 if(anonFd == -1)
187 {
188 mapping = mmap(nullptr, length, PROT_READ | PROT_WRITE,
189 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
190 }
191 else
192 {
193 ensureAnonFileSize(anonFd, length);
194 mapping = mmap(nullptr, length, PROT_READ | PROT_WRITE,
195 MAP_PRIVATE, anonFd, 0);
196 }
197
198 if(mapping == MAP_FAILED)
199 {
200 mapping = nullptr;
201 }
202 #else
203 mapping = allocate(length, pageSize);
204 #endif
205
206 return mapping;
207}
208
209void markExecutable(void *memory, size_t bytes)
210{
211 #if defined(_WIN32)
212 unsigned long oldProtection;
213 VirtualProtect(memory, bytes, PAGE_EXECUTE_READ, &oldProtection);
214 #else
215 mprotect(memory, bytes, PROT_READ | PROT_EXEC);
216 #endif
217}
218
219void deallocateExecutable(void *memory, size_t bytes)
220{
221 #if defined(_WIN32)
222 unsigned long oldProtection;
223 VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection);
224 deallocate(memory);
225 #elif defined(LINUX_ENABLE_NAMED_MMAP)
226 size_t pageSize = memoryPageSize();
227 size_t length = (bytes + pageSize - 1) & ~(pageSize - 1);
228 munmap(memory, length);
229 #else
230 mprotect(memory, bytes, PROT_READ | PROT_WRITE);
231 deallocate(memory);
232 #endif
233}
234
235void clear(uint16_t *memory, uint16_t element, size_t count)
236{
237 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
238 __stosw(memory, element, count);
239 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
240 __asm__("rep stosw" : : "D"(memory), "a"(element), "c"(count));
241 #else
242 for(size_t i = 0; i < count; i++)
243 {
244 memory[i] = element;
245 }
246 #endif
247}
248
249void clear(uint32_t *memory, uint32_t element, size_t count)
250{
251 #if defined(_MSC_VER) && defined(__x86__) && !defined(MEMORY_SANITIZER)
252 __stosd((unsigned long*)memory, element, count);
253 #elif defined(__GNUC__) && defined(__x86__) && !defined(MEMORY_SANITIZER)
254 __asm__("rep stosl" : : "D"(memory), "a"(element), "c"(count));
255 #else
256 for(size_t i = 0; i < count; i++)
257 {
258 memory[i] = element;
259 }
260 #endif
261}
262}