1 /* 2 * Capability allocation and management. 3 * 4 * Copyright (C) 2019, 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/c/util/cap_alloc.h> 23 24 #include "cap_alloc.h" 25 #include "semaphore.h" 26 27 28 29 /* Semaphore used if threading has been initialised by ipc_cap_alloc_init. */ 30 31 l4_cap_idx_t ipc_cap_alloc_semaphore = L4_INVALID_CAP; 32 33 34 35 /* Reserve and return a capability index. */ 36 37 l4_cap_idx_t ipc_cap_alloc() 38 { 39 l4_cap_idx_t cap; 40 long err; 41 42 /* Allocate a capability in the critical section, then return. */ 43 44 err = ipc_semaphore_down(ipc_cap_alloc_semaphore); 45 if (err) 46 return L4_INVALID_CAP; 47 48 cap = l4re_util_cap_alloc(); 49 50 ipc_semaphore_up(ipc_cap_alloc_semaphore); 51 52 return cap; 53 } 54 55 /* Release a capability. */ 56 57 int ipc_cap_free(l4_cap_idx_t cap) 58 { 59 long err; 60 61 err = ipc_semaphore_down(ipc_cap_alloc_semaphore); 62 if (err) 63 return 0; 64 65 l4re_util_cap_free(cap); 66 67 ipc_semaphore_up(ipc_cap_alloc_semaphore); 68 69 return 1; 70 } 71 72 /* Free the indicated capability and unmap it. */ 73 74 int ipc_cap_free_um(l4_cap_idx_t cap) 75 { 76 long err; 77 78 err = ipc_semaphore_down(ipc_cap_alloc_semaphore); 79 if (err) 80 return 0; 81 82 l4re_util_cap_free_um(cap); 83 84 ipc_semaphore_up(ipc_cap_alloc_semaphore); 85 86 return 1; 87 } 88 89 // vim: tabstop=2 expandtab shiftwidth=2