Merge commit 'origin/master' into gallium-sampler-view
[mesa.git] / src / mesa / glapi / glapi_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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file glapi_execmem.c
28 *
29 * Function for allocating executable memory for dispatch stubs.
30 *
31 * Copied from main/execmem.c and simplified for dispatch stubs.
32 */
33
34
35 #ifdef HAVE_DIX_CONFIG_H
36 #include <dix-config.h>
37 #include "glapi/mesa.h"
38 #else
39 #include "main/compiler.h"
40 #endif
41
42 #include "glapi/glthread.h"
43
44
45 #if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun)
46
47 #include <unistd.h>
48 #include <sys/mman.h>
49
50 #ifdef MESA_SELINUX
51 #include <selinux/selinux.h>
52 #endif
53
54
55 #ifndef MAP_ANONYMOUS
56 #define MAP_ANONYMOUS MAP_ANON
57 #endif
58
59
60 #define EXEC_MAP_SIZE (4*1024)
61
62 _glthread_DECLARE_STATIC_MUTEX(exec_mutex);
63
64 static unsigned int head = 0;
65
66 static unsigned char *exec_mem = NULL;
67
68
69 /*
70 * Dispatch stubs are of fixed size and never freed. Thus, we do not need to
71 * overlay a heap, we just mmap a page and manage through an index.
72 */
73
74 static int
75 init_map(void)
76 {
77 #ifdef MESA_SELINUX
78 if (is_selinux_enabled()) {
79 if (!security_get_boolean_active("allow_execmem") ||
80 !security_get_boolean_pending("allow_execmem"))
81 return 0;
82 }
83 #endif
84
85 if (!exec_mem)
86 exec_mem = mmap(NULL, EXEC_MAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
87 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
88
89 return (exec_mem != MAP_FAILED);
90 }
91
92
93 void *
94 _glapi_exec_malloc(unsigned int size)
95 {
96 void *addr = NULL;
97
98 _glthread_LOCK_MUTEX(exec_mutex);
99
100 if (!init_map())
101 goto bail;
102
103 /* free space check, assumes no integer overflow */
104 if (head + size > EXEC_MAP_SIZE)
105 goto bail;
106
107 /* allocation, assumes proper addr and size alignement */
108 addr = exec_mem + head;
109 head += size;
110
111 bail:
112 _glthread_UNLOCK_MUTEX(exec_mutex);
113
114 return addr;
115 }
116
117
118 #else
119
120 void *
121 _glapi_exec_malloc(unsigned int size)
122 {
123 return malloc(size);
124 }
125
126
127 #endif