1848b8605Smrg/* 2848b8605Smrg * Mesa 3-D graphics library 3848b8605Smrg * 4848b8605Smrg * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 5848b8605Smrg * 6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7848b8605Smrg * copy of this software and associated documentation files (the "Software"), 8848b8605Smrg * to deal in the Software without restriction, including without limitation 9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the 11848b8605Smrg * Software is furnished to do so, subject to the following conditions: 12848b8605Smrg * 13848b8605Smrg * The above copyright notice and this permission notice shall be included 14848b8605Smrg * in all copies or substantial portions of the Software. 15848b8605Smrg * 16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE. 23848b8605Smrg */ 24848b8605Smrg 25848b8605Smrg 26848b8605Smrg/** 27848b8605Smrg * \file glapi_execmem.c 28848b8605Smrg * 29848b8605Smrg * Function for allocating executable memory for dispatch stubs. 30848b8605Smrg * 31848b8605Smrg * Copied from main/execmem.c and simplified for dispatch stubs. 32848b8605Smrg */ 33848b8605Smrg 34848b8605Smrg 35b8e80941Smrg#include "c99_compat.h" 36b8e80941Smrg#include "c11/threads.h" 37848b8605Smrg#include "u_execmem.h" 38848b8605Smrg 39848b8605Smrg 40848b8605Smrg#define EXEC_MAP_SIZE (4*1024) 41848b8605Smrg 42848b8605Smrgstatic mtx_t exec_mutex = _MTX_INITIALIZER_NP; 43848b8605Smrg 44848b8605Smrgstatic unsigned int head = 0; 45848b8605Smrg 46848b8605Smrgstatic unsigned char *exec_mem = (unsigned char *)0; 47848b8605Smrg 48848b8605Smrg 49848b8605Smrg#if defined(__linux__) || defined(__OpenBSD__) || defined(_NetBSD__) || defined(__sun) || defined(__HAIKU__) 50848b8605Smrg 51848b8605Smrg#include <unistd.h> 52848b8605Smrg#include <sys/mman.h> 53848b8605Smrg 54848b8605Smrg#ifdef MESA_SELINUX 55848b8605Smrg#include <selinux/selinux.h> 56848b8605Smrg#endif 57848b8605Smrg 58848b8605Smrg 59848b8605Smrg#ifndef MAP_ANONYMOUS 60848b8605Smrg#define MAP_ANONYMOUS MAP_ANON 61848b8605Smrg#endif 62848b8605Smrg 63848b8605Smrg 64848b8605Smrg/* 65848b8605Smrg * Dispatch stubs are of fixed size and never freed. Thus, we do not need to 66848b8605Smrg * overlay a heap, we just mmap a page and manage through an index. 67848b8605Smrg */ 68848b8605Smrg 69848b8605Smrgstatic int 70848b8605Smrginit_map(void) 71848b8605Smrg{ 72848b8605Smrg#ifdef MESA_SELINUX 73848b8605Smrg if (is_selinux_enabled()) { 74848b8605Smrg if (!security_get_boolean_active("allow_execmem") || 75848b8605Smrg !security_get_boolean_pending("allow_execmem")) 76848b8605Smrg return 0; 77848b8605Smrg } 78848b8605Smrg#endif 79848b8605Smrg 80848b8605Smrg if (!exec_mem) 81848b8605Smrg exec_mem = mmap(NULL, EXEC_MAP_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, 82848b8605Smrg MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 83848b8605Smrg 84848b8605Smrg return (exec_mem != MAP_FAILED); 85848b8605Smrg} 86848b8605Smrg 87848b8605Smrg 88848b8605Smrg#elif defined(_WIN32) 89848b8605Smrg 90848b8605Smrg#include <windows.h> 91848b8605Smrg 92848b8605Smrg 93848b8605Smrg/* 94848b8605Smrg * Avoid Data Execution Prevention. 95848b8605Smrg */ 96848b8605Smrg 97848b8605Smrgstatic int 98848b8605Smrginit_map(void) 99848b8605Smrg{ 100848b8605Smrg exec_mem = VirtualAlloc(NULL, EXEC_MAP_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE); 101848b8605Smrg 102848b8605Smrg return (exec_mem != NULL); 103848b8605Smrg} 104848b8605Smrg 105848b8605Smrg 106848b8605Smrg#else 107848b8605Smrg 108848b8605Smrg#include <stdlib.h> 109848b8605Smrg 110848b8605Smrgstatic int 111848b8605Smrginit_map(void) 112848b8605Smrg{ 113848b8605Smrg exec_mem = malloc(EXEC_MAP_SIZE); 114848b8605Smrg 115848b8605Smrg return (exec_mem != NULL); 116848b8605Smrg} 117848b8605Smrg 118848b8605Smrg 119848b8605Smrg#endif 120848b8605Smrg 121848b8605Smrgvoid * 122848b8605Smrgu_execmem_alloc(unsigned int size) 123848b8605Smrg{ 124848b8605Smrg void *addr = NULL; 125848b8605Smrg 126848b8605Smrg mtx_lock(&exec_mutex); 127848b8605Smrg 128848b8605Smrg if (!init_map()) 129848b8605Smrg goto bail; 130848b8605Smrg 131848b8605Smrg /* free space check, assumes no integer overflow */ 132848b8605Smrg if (head + size > EXEC_MAP_SIZE) 133848b8605Smrg goto bail; 134848b8605Smrg 135848b8605Smrg /* allocation, assumes proper addr and size alignement */ 136848b8605Smrg addr = exec_mem + head; 137848b8605Smrg head += size; 138848b8605Smrg 139848b8605Smrgbail: 140848b8605Smrg mtx_unlock(&exec_mutex); 141848b8605Smrg 142848b8605Smrg return addr; 143848b8605Smrg} 144848b8605Smrg 145848b8605Smrg 146