execmem.c revision af69d88d
1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 26/** 27 * \file execmem.c 28 * Functions for allocating executable memory. 29 * 30 * \author Keith Whitwell 31 */ 32 33 34#include "imports.h" 35 36 37 38#if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun) || defined(__HAIKU__) 39 40/* 41 * Allocate a large block of memory which can hold code then dole it out 42 * in pieces by means of the generic memory manager code. 43*/ 44 45#include <unistd.h> 46#include <sys/mman.h> 47#include "mm.h" 48 49#ifdef MESA_SELINUX 50#include <selinux/selinux.h> 51#endif 52 53 54#ifndef MAP_ANONYMOUS 55#define MAP_ANONYMOUS MAP_ANON 56#endif 57 58 59#define EXEC_HEAP_SIZE (10*1024*1024) 60 61static mtx_t exec_mutex = _MTX_INITIALIZER_NP; 62 63static struct mem_block *exec_heap = NULL; 64static unsigned char *exec_mem = NULL; 65 66 67static int 68init_heap(void) 69{ 70#ifdef MESA_SELINUX 71 if (is_selinux_enabled()) { 72 if (!security_get_boolean_active("allow_execmem") || 73 !security_get_boolean_pending("allow_execmem")) 74 return 0; 75 } 76#endif 77 78 if (!exec_heap) 79 exec_heap = mmInit( 0, EXEC_HEAP_SIZE ); 80 81 if (!exec_mem) 82 exec_mem = mmap(NULL, EXEC_HEAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, 83 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 84 85 return (exec_mem != MAP_FAILED); 86} 87 88 89void * 90_mesa_exec_malloc(GLuint size) 91{ 92 struct mem_block *block = NULL; 93 void *addr = NULL; 94 95 mtx_lock(&exec_mutex); 96 97 if (!init_heap()) 98 goto bail; 99 100 if (exec_heap) { 101 size = (size + 31) & ~31; 102 block = mmAllocMem( exec_heap, size, 32, 0 ); 103 } 104 105 if (block) 106 addr = exec_mem + block->ofs; 107 else 108 printf("_mesa_exec_malloc failed\n"); 109 110bail: 111 mtx_unlock(&exec_mutex); 112 113 return addr; 114} 115 116 117void 118_mesa_exec_free(void *addr) 119{ 120 mtx_lock(&exec_mutex); 121 122 if (exec_heap) { 123 struct mem_block *block = mmFindBlock(exec_heap, (unsigned char *)addr - exec_mem); 124 125 if (block) 126 mmFreeMem(block); 127 } 128 129 mtx_unlock(&exec_mutex); 130} 131 132 133#else 134 135/* 136 * Just use regular memory. 137 */ 138 139void * 140_mesa_exec_malloc(GLuint size) 141{ 142 return malloc( size ); 143} 144 145 146void 147_mesa_exec_free(void *addr) 148{ 149 free(addr); 150} 151 152 153#endif 154