xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/rtasm/rtasm_execmem.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  **************************************************************************/
24 
25 
26 /**
27  * \file exemem.c
28  * Functions for allocating executable memory.
29  *
30  * \author Keith Whitwell
31  */
32 
33 
34 #include "util/compiler.h"
35 #include "util/simple_mtx.h"
36 #include "util/u_debug.h"
37 #include "util/u_thread.h"
38 #include "util/u_memory.h"
39 
40 #include "rtasm_execmem.h"
41 
42 #ifndef MAP_ANONYMOUS
43 #define MAP_ANONYMOUS MAP_ANON
44 #endif
45 
46 #if DETECT_OS_WINDOWS
47 #include <windows.h>
48 #endif
49 
50 #if DETECT_OS_POSIX
51 
52 
53 /*
54  * Allocate a large block of memory which can hold code then dole it out
55  * in pieces by means of the generic memory manager code.
56  */
57 
58 #include <unistd.h>
59 #include <sys/mman.h>
60 #include "util/u_mm.h"
61 
62 #define EXEC_HEAP_SIZE (10*1024*1024)
63 
64 static simple_mtx_t exec_mutex = SIMPLE_MTX_INITIALIZER;
65 
66 static struct mem_block *exec_heap = NULL;
67 static unsigned char *exec_mem = NULL;
68 
69 
70 static int
init_heap(void)71 init_heap(void)
72 {
73    if (!exec_heap)
74       exec_heap = u_mmInit( 0, EXEC_HEAP_SIZE );
75 
76    if (!exec_mem)
77       exec_mem = (unsigned char *) mmap(NULL, EXEC_HEAP_SIZE,
78          PROT_EXEC | PROT_READ | PROT_WRITE,
79          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
80 
81    return (exec_mem != MAP_FAILED);
82 }
83 
84 
85 void *
rtasm_exec_malloc(size_t size)86 rtasm_exec_malloc(size_t size)
87 {
88    struct mem_block *block = NULL;
89    void *addr = NULL;
90 
91    simple_mtx_lock(&exec_mutex);
92 
93    if (!init_heap())
94       goto bail;
95 
96    if (exec_heap) {
97       size = (size + 31) & ~31;  /* next multiple of 32 bytes */
98       block = u_mmAllocMem( exec_heap, size, 5, 0 ); /* 5 -> 32-byte alignment */
99    }
100 
101    if (block)
102       addr = exec_mem + block->ofs;
103    else
104       debug_printf("rtasm_exec_malloc failed\n");
105 
106 bail:
107    simple_mtx_unlock(&exec_mutex);
108 
109    return addr;
110 }
111 
112 
113 void
rtasm_exec_free(void * addr)114 rtasm_exec_free(void *addr)
115 {
116    simple_mtx_lock(&exec_mutex);
117 
118    if (exec_heap) {
119       struct mem_block *block = u_mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem);
120 
121       if (block)
122          u_mmFreeMem(block);
123    }
124 
125    simple_mtx_unlock(&exec_mutex);
126 }
127 
128 
129 #elif DETECT_OS_WINDOWS
130 
131 
132 /*
133  * Avoid Data Execution Prevention.
134  */
135 
136 void *
rtasm_exec_malloc(size_t size)137 rtasm_exec_malloc(size_t size)
138 {
139    return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
140 }
141 
142 
143 void
rtasm_exec_free(void * addr)144 rtasm_exec_free(void *addr)
145 {
146    VirtualFree(addr, 0, MEM_RELEASE);
147 }
148 
149 
150 #else
151 
152 
153 /*
154  * Just use regular memory.
155  */
156 
157 void *
rtasm_exec_malloc(size_t size)158 rtasm_exec_malloc(size_t size)
159 {
160    return MALLOC( size );
161 }
162 
163 
164 void
rtasm_exec_free(void * addr)165 rtasm_exec_free(void *addr)
166 {
167    FREE(addr);
168 }
169 
170 
171 #endif
172