1 /* Copyright (C) 2010-2020 The RetroArch team
2 *
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (memmap.c).
5 * ---------------------------------------------------------------------------------------
6 *
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <stdint.h>
24 #include <memmap.h>
25
26 #ifndef PROT_READ
27 #define PROT_READ 0x1 /* Page can be read */
28 #endif
29
30 #ifndef PROT_WRITE
31 #define PROT_WRITE 0x2 /* Page can be written. */
32 #endif
33
34 #ifndef PROT_READWRITE
35 #define PROT_READWRITE 0x3 /* Page can be written to and read from. */
36 #endif
37
38 #ifndef PROT_EXEC
39 #define PROT_EXEC 0x4 /* Page can be executed. */
40 #endif
41
42 #ifndef PROT_NONE
43 #define PROT_NONE 0x0 /* Page can not be accessed. */
44 #endif
45
46 #ifndef MAP_FAILED
47 #define MAP_FAILED ((void *) -1)
48 #endif
49
50 #ifdef _WIN32
mmap(void * addr,size_t len,int prot,int flags,int fildes,size_t offset)51 void* mmap(void *addr, size_t len, int prot, int flags,
52 int fildes, size_t offset)
53 {
54 void *map = (void*)NULL;
55 HANDLE handle = INVALID_HANDLE_VALUE;
56
57 switch (prot)
58 {
59 case PROT_READ:
60 default:
61 handle = CreateFileMapping((HANDLE)
62 _get_osfhandle(fildes), 0, PAGE_READONLY, 0,
63 len, 0);
64 if (!handle)
65 break;
66 map = (void*)MapViewOfFile(handle, FILE_MAP_READ, 0, 0, len);
67 CloseHandle(handle);
68 break;
69 case PROT_WRITE:
70 handle = CreateFileMapping((HANDLE)
71 _get_osfhandle(fildes),0,PAGE_READWRITE,0,
72 len, 0);
73 if (!handle)
74 break;
75 map = (void*)MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, len);
76 CloseHandle(handle);
77 break;
78 case PROT_READWRITE:
79 handle = CreateFileMapping((HANDLE)
80 _get_osfhandle(fildes),0,PAGE_READWRITE,0,
81 len, 0);
82 if (!handle)
83 break;
84 map = (void*)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, len);
85 CloseHandle(handle);
86 break;
87 }
88
89 if (map == (void*)NULL)
90 return((void*)MAP_FAILED);
91 return((void*) ((int8_t*)map + offset));
92 }
93
munmap(void * addr,size_t length)94 int munmap(void *addr, size_t length)
95 {
96 if (!UnmapViewOfFile(addr))
97 return -1;
98 return 0;
99 }
100
mprotect(void * addr,size_t len,int prot)101 int mprotect(void *addr, size_t len, int prot)
102 {
103 /* Incomplete, just assumes PAGE_EXECUTE_READWRITE right now
104 * instead of correctly handling prot */
105 prot = 0;
106 if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
107 prot = PAGE_EXECUTE_READWRITE;
108 return VirtualProtect(addr, len, prot, 0);
109 }
110
111 #elif !defined(HAVE_MMAN)
mmap(void * addr,size_t len,int prot,int flags,int fildes,size_t offset)112 void* mmap(void *addr, size_t len, int prot, int flags,
113 int fildes, size_t offset)
114 {
115 return malloc(len);
116 }
117
munmap(void * addr,size_t len)118 int munmap(void *addr, size_t len)
119 {
120 free(addr);
121 return 0;
122 }
123
mprotect(void * addr,size_t len,int prot)124 int mprotect(void *addr, size_t len, int prot)
125 {
126 /* stub - not really needed at this point
127 * since this codepath has no dynarecs. */
128 return 0;
129 }
130
131 #endif
132
133 #if defined(__MACH__) && defined(__arm__)
134 #include <libkern/OSCacheControl.h>
135 #endif
136
memsync(void * start,void * end)137 int memsync(void *start, void *end)
138 {
139 size_t len = (char*)end - (char*)start;
140 #if defined(__MACH__) && defined(__arm__)
141 sys_dcache_flush(start ,len);
142 sys_icache_invalidate(start, len);
143 return 0;
144 #elif defined(__arm__) && !defined(__QNX__)
145 (void)len;
146 __clear_cache(start, end);
147 return 0;
148 #elif defined(HAVE_MMAN)
149 return msync(start, len, MS_SYNC | MS_INVALIDATE
150 #ifdef __QNX__
151 MS_CACHE_ONLY
152 #endif
153 );
154 #else
155 (void)len;
156 return 0;
157 #endif
158 }
159
memprotect(void * addr,size_t len)160 int memprotect(void *addr, size_t len)
161 {
162 return mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_EXEC);
163 }
164