mesa: Restore 78-column wrapping of license text in C-style comments.
[mesa.git] / src / mapi / u_execmem.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file glapi_execmem.c
29 *
30 * Function for allocating executable memory for dispatch stubs.
31 *
32 * Copied from main/execmem.c and simplified for dispatch stubs.
33 */
34
35
36 #include "u_compiler.h"
37 #include "u_thread.h"
38 #include "u_execmem.h"
39
40
41 #define EXEC_MAP_SIZE (4*1024)
42
43 u_mutex_declare_static(exec_mutex);
44
45 static unsigned int head = 0;
46
47 static unsigned char *exec_mem = (unsigned char *)0;
48
49
50 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun)
51
52 #include <unistd.h>
53 #include <sys/mman.h>
54
55 #ifdef MESA_SELINUX
56 #include <selinux/selinux.h>
57 #endif
58
59
60 #ifndef MAP_ANONYMOUS
61 #define MAP_ANONYMOUS MAP_ANON
62 #endif
63
64
65 /*
66 * Dispatch stubs are of fixed size and never freed. Thus, we do not need to
67 * overlay a heap, we just mmap a page and manage through an index.
68 */
69
70 static int
71 init_map(void)
72 {
73 #ifdef MESA_SELINUX
74 if (is_selinux_enabled()) {
75 if (!security_get_boolean_active("allow_execmem") ||
76 !security_get_boolean_pending("allow_execmem"))
77 return 0;
78 }
79 #endif
80
81 if (!exec_mem)
82 exec_mem = mmap(NULL, EXEC_MAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
83 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
84
85 return (exec_mem != MAP_FAILED);
86 }
87
88
89 #elif defined(_WIN32)
90
91 #include <windows.h>
92
93
94 /*
95 * Avoid Data Execution Prevention.
96 */
97
98 static int
99 init_map(void)
100 {
101 exec_mem = VirtualAlloc(NULL, EXEC_MAP_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
102
103 return (exec_mem != NULL);
104 }
105
106
107 #else
108
109 #include <stdlib.h>
110
111 static int
112 init_map(void)
113 {
114 exec_mem = malloc(EXEC_MAP_SIZE);
115
116 return (exec_mem != NULL);
117 }
118
119
120 #endif
121
122 void *
123 u_execmem_alloc(unsigned int size)
124 {
125 void *addr = NULL;
126
127 u_mutex_lock(exec_mutex);
128
129 if (!init_map())
130 goto bail;
131
132 /* free space check, assumes no integer overflow */
133 if (head + size > EXEC_MAP_SIZE)
134 goto bail;
135
136 /* allocation, assumes proper addr and size alignement */
137 addr = exec_mem + head;
138 head += size;
139
140 bail:
141 u_mutex_unlock(exec_mutex);
142
143 return addr;
144 }
145
146